import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'
import BasicLayout from '@/layouts/BasicLayout.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/LoginView.vue'),
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('../views/RegisterView.vue'),
    },
    {
      path: '/',
      component: BasicLayout,
      children: [
        {
          path: '', // 默认首页
          name: 'home',
          component: () => import('../views/HomeView.vue'),
        },
        {
          path: '/property-charge',
          name: 'property-charge',
          component: () => import('../views/PropertyChargeView.vue'),
        },
        {
          path: '/parking-charge',
          name: 'parking-charge',
          component: () => import('../views/ParkingChargeView.vue'),
        },
        {
          path: '/complaint',
          name: 'complaint',
          component: () => import('../views/ComplaintView.vue'),
        },
        {
          path: '/repair',
          name: 'repair',
          component: () => import('../views/RepairView.vue'),
        },
        {
          path: '/building',
          name: 'building',
          component: () => import('../views/BuildingView.vue'),
        },
        {
          path: '/house',
          name: 'house',
          component: () => import('../views/HouseView.vue'),
        },
        {
          path: '/owner',
          name: 'owner',
          component: () => import('../views/OwnerView.vue'),
        },
        {
          path: '/parking',
          name: 'parking',
          component: () => import('../views/ParkingView.vue'),
        },
        {
          path: '/meter-reading',
          name: 'meter-reading',
          component: () => import('../views/MeterReadingView.vue'),
        },
        {
          path: '/user',
          name: 'user',
          component: () => import('../views/UserView.vue'),
        },
        {
          path: '/role',
          name: 'role',
          component: () => import('../views/RoleView.vue'),
        },
        {
          path: '/menu',
          name: 'menu',
          component: () => import('../views/MenuView.vue'),
        },
        {
          path: '/statistical-analysis',
          name: 'statistical-analysis',
          component: () => import('../views/StatisticalAnalysisView.vue'),
        },
      ],
    },
  ],
})

router.beforeEach(async (to) => {
  const userStore = useUserStore()
  if (to.name !== 'login' && to.name !== 'register' && !userStore.isLogin) {
    return { name: 'login' }
  }
})

export default router
